package com.liip.platform.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.liip.platform.exception.RedisException;
import com.liip.platform.service.RedisService;

import lombok.extern.slf4j.Slf4j;

/**
 * @description Redis服务
 * @author 罗林
 * @date 2020-03-31 13:23:16
 */
@Slf4j
@Service
public class RedisServiceImpl implements RedisService {

	@Resource
	private StringRedisTemplate strRedisTemplate;

	@Override
	public Set<String> keys(String keys) {
		try {
			return strRedisTemplate.keys(keys);
		} catch (Exception e) {
			try {
				throw new RedisException(e.getMessage());
			} catch (RedisException e1) {
				e1.printStackTrace();
			}
			return null;
		}
	}

	@Override
	public boolean expire(String key, long time) {
		try {
			if (time > 0) {
				strRedisTemplate.expire(key, time, TimeUnit.SECONDS);
			}
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public long getExpire(String key) {
		return strRedisTemplate.getExpire(key, TimeUnit.SECONDS);
	}

	@Override
	public boolean hasKey(String key) {
		try {
			return strRedisTemplate.hasKey(key);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public void del(String... key) {
		if (key != null && key.length > 0) {
			if (key.length == 1) {
				strRedisTemplate.delete(key[0]);
			} else {
				strRedisTemplate.delete(CollectionUtils.arrayToList(key));
			}
		}
	}

	@Override
	public Object get(String key) {
		return key == null ? null : strRedisTemplate.opsForValue().get(key);
	}

	@Override
	public boolean set(String key, String value) {
		try {
			strRedisTemplate.opsForValue().set(key, value);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public boolean set(String key, String value, long time) {
		try {
			if (time > 0) {
				strRedisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
			} else {
				set(key, value);
			}
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public long incr(String key, long delta) {
		if (delta < 0) {
			throw new RuntimeException("递增因子必须大于0");
		}
		return strRedisTemplate.opsForValue().increment(key, delta);
	}

	@Override
	public long decr(String key, long delta) {
		if (delta < 0) {
			throw new RuntimeException("递减因子必须大于0");
		}
		return strRedisTemplate.opsForValue().increment(key, -delta);
	}

	@Override
	public Object hget(String key, String item) {
		return strRedisTemplate.opsForHash().get(key, item);
	}

	@Override
	public Map<Object, Object> hmget(String key) {
		return strRedisTemplate.opsForHash().entries(key);
	}

	@Override
	public boolean hmset(String key, Map<String, Object> map) {
		try {
			strRedisTemplate.opsForHash().putAll(key, map);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public boolean hmset(String key, Map<String, Object> map, long time) {
		try {
			strRedisTemplate.opsForHash().putAll(key, map);
			if (time > 0) {
				expire(key, time);
			}
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public boolean hset(String key, String item, Object value) {
		try {
			strRedisTemplate.opsForHash().put(key, item, value);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public boolean hset(String key, String item, Object value, long time) {
		try {
			strRedisTemplate.opsForHash().put(key, item, value);
			if (time > 0) {
				expire(key, time);
			}
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public void hdel(String key, Object... item) {
		strRedisTemplate.opsForHash().delete(key, item);
	}

	@Override
	public boolean hHasKey(String key, String item) {
		return strRedisTemplate.opsForHash().hasKey(key, item);
	}

	@Override
	public double hincr(String key, String item, double by) {
		return strRedisTemplate.opsForHash().increment(key, item, by);
	}

	@Override
	public double hdecr(String key, String item, double by) {
		return strRedisTemplate.opsForHash().increment(key, item, -by);
	}

	@Override
	public Set<String> sGet(String key) {
		try {
			return strRedisTemplate.opsForSet().members(key);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public boolean sHasKey(String key, Object value) {
		try {
			return strRedisTemplate.opsForSet().isMember(key, value);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public long sSet(String key, String... values) {
		try {
			return strRedisTemplate.opsForSet().add(key, values);
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}

	@Override
	public long sSetAndTime(String key, long time, String... values) {
		try {
			Long count = strRedisTemplate.opsForSet().add(key, values);
			if (time > 0)
				expire(key, time);
			return count;
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}

	@Override
	public long sGetSetSize(String key) {
		try {
			return strRedisTemplate.opsForSet().size(key);
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}

	@Override
	public long setRemove(String key, Object... values) {
		try {
			Long count = strRedisTemplate.opsForSet().remove(key, values);
			return count;
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}
	// ===============================list=================================
	
	@Override
	public Object rPop(String key){
		return strRedisTemplate.opsForList().rightPop(key);
	}
	
	@Override
	public void lTrim(String key, long start, long end) {
		strRedisTemplate.opsForList().trim(key, start, end);
	}
	@Override
	public List<String> lGet(String key, long start, long end) {
		try {
			return strRedisTemplate.opsForList().range(key, start, end);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public long lGetListSize(String key) {
		try {
			return strRedisTemplate.opsForList().size(key);
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}

	@Override
	public Object lGetIndex(String key, long index) {
		try {
			return strRedisTemplate.opsForList().index(key, index);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public boolean lSet(String key, String value) {
		try {
			strRedisTemplate.opsForList().leftPush(key, value);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public boolean lSet(String key, String value, long time) {
		try {
			strRedisTemplate.opsForList().leftPush(key, value);
			if (time > 0)
				expire(key, time);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public boolean lSet(String key, List<String> value) {
		try {
			strRedisTemplate.opsForList().rightPushAll(key, value);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public boolean lSet(String key, List<String> value, long time) {
		try {
			strRedisTemplate.opsForList().rightPushAll(key, value);
			if (time > 0)
				expire(key, time);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public boolean lUpdateIndex(String key, long index, String value) {
		try {
			strRedisTemplate.opsForList().set(key, index, value);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public long lRemove(String key, long count, String value) {
		try {
			Long remove = strRedisTemplate.opsForList().remove(key, count, value);
			return remove;
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}

	@Override
	public boolean checkConnection() {
		RedisConnection redisConnection = strRedisTemplate.getConnectionFactory().getConnection();
		Boolean flag = redisConnection.isClosed();
		if(!flag) {
			flag = true;
		}
		return flag;
	}

}
